﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class Inventory : EFViewCmes
    {
        public Inventory(String dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="type"></param>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.fdDepotId.IsEnabled = false;
                    this.fdInventoryCode.IsReadOnly = true;
                    this.fdInventoryDate.IsReadOnly = true;
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCM50", sender, e);
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void fdDepotId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            EFDataField field = sender as EFDataField;
            EFComboBox box = field.Content as EFComboBox;
            if (box.SelectedItem == null)
            {               
                
                return;
            }
            string fdDepotCode = box.SelectedItem.GetType().GetProperty("fdDepotCode").GetValue(box.SelectedItem, null) as string;

            this.fdDepotCode.Value = fdDepotCode;
        }
    }

}
